package com.dongdongshop.Service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.GoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public Boolean addGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem) {
        try{
            //增加返回主键id
            Integer a = tbGoodsMapper.insertTbGoodsId(tbGoods);
            Long id1 = tbGoods.getId();
            //tbGoodsDesc主键没有自增需要返回主键手动赋值
            tbGoodsDesc.setGoodsId(id1);
            Integer b = tbGoodsDescMapper.insert(tbGoodsDesc);
            tbItem.setGoodsId(id1);
            Integer c = tbItemMapper.insert(tbItem);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Page<TbGoods> goodsList(String goodsName,Integer pageNumer,Integer pageSize) {
        PageHelper.startPage(pageNumer,pageSize);
        List<TbGoods> list = tbGoodsMapper.goodsList(goodsName);
        PageInfo<TbGoods> page = new PageInfo<>(list);
        return new Page<TbGoods>(page.getPageNum(),page.getPageSize(),page.getTotal(),page.getPages(),page.getList());
    }

    @Override
    public Integer upStatus(Long[] ids) {
        for (Long id : ids) {
            TbGoods tbGoods = new TbGoods();
            tbGoods.setId(id);
            tbGoods.setAuditStatus("2");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        }
        return 1;
    }

    @Override
    public Integer outStatus(Long[] ids) {
        return tbGoodsMapper.outStatus(ids);
    }

    @Override
    public TbGoods selectGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }
}
